package com.toby.day44.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.toby.day44.R;
import com.toby.day44.bean.Product;

import java.util.List;

/**
 * @author zhangwei
 * @version V1.0
 * @Title: Advanced
 * @package com.toby.day44.adapter
 * @Description:
 * @date 15/10/27 下午11:40
 */

public class CarBusAdapter extends BaseAdapter implements View.OnClickListener {
    private List<Product> products;
    private LayoutInflater inflater;

    public CarBusAdapter(List<Product> products, Context context) {
        this.products = products;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return products == null ? 0 : products.size();
    }

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Product p = (Product) getItem(position);
        ViewHolder vh;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_car_layout, parent, false);
            vh = new ViewHolder(convertView);
            vh.addBtn.setOnClickListener(this);
            vh.deleteBtn.setOnClickListener(this);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.addBtn.setTag(position);
        vh.deleteBtn.setTag(position);
        vh.img.setAspectRatio(1.0f);
        vh.img.setImageURI(Uri.parse(p.getPhoto().getHuge()));
        vh.qtyTv.setText(String.valueOf(products.get(position).getQty()));
        vh.nameTv.setText(p.getName());
        vh.specTv.setText(p.getSpec());
        return convertView;
    }

    @Override
    public void onClick(View v) {
        int postion = v.getTag() instanceof Integer ? (int) v.getTag() : 0;
        Product product = products.get(postion);
        switch (v.getId()) {
            case R.id.car_add:
                product.setQty(product.getQty() + 1);
                break;
            case R.id.car_detele:
                if (product.getQty() > 0) {
                    product.setQty(product.getQty() - 1);
                }
                break;
        }
        this.notifyDataSetChanged();
    }

    private static class ViewHolder {
        private SimpleDraweeView img;
        private TextView nameTv;
        private TextView specTv;
        private TextView priceTv;
        private Button deleteBtn;
        private Button addBtn;
        private TextView qtyTv;

        public ViewHolder(View contVertView) {
            img = (SimpleDraweeView) contVertView.findViewById(R.id.car_img);
            nameTv = (TextView) contVertView.findViewById(R.id.car_title);
            specTv = (TextView) contVertView.findViewById(R.id.car_spec);
            priceTv = (TextView) contVertView.findViewById(R.id.car_price);
            deleteBtn = (Button) contVertView.findViewById(R.id.car_detele);
            addBtn = (Button) contVertView.findViewById(R.id.car_add);
            qtyTv = (TextView) contVertView.findViewById(R.id.car_qty);
        }

    }


}
